var chek = 'root';
var grid = null;

$(function() {	
	grid = lyGrid({
		pagId : 'paging',

		l_column : [
            { colkey : "dept_name", name : "部门名称", isSort:true ,align:"left"}, 
            { colkey : "dept_code", name : "部门编号", width:'150px' },  
            { colkey : "dept_type", name : "部门类型", isSort:true, width:'150px',
            	renderData : function(rowindex,data, rowdata, column) { return (data+'')=='2'?'部门':'公司' }
            },
            { colkey : "dept_state",name : "状态", width : '80px', isSort:true,
            	renderData : function(rowindex,data, rowdata, column) { return (data+'')=='0'?'启用':'禁用' }
            }, 
            { colkey : "sequence", name : "排序" , width : '80px'},
            { colkey : "setting_name", name : "话务平台" , width : '150px'},
            { colkey : "create_time", name : "创建时间", isSort:true, width : '180px',
        		renderData : function(rowindex,data, rowdata, column) { return new Date(data).format("yyyy-MM-dd hh:mm"); }
            }
        ],
		jsonUrl : rootPath + '/system/dept/findByPage.shtml?SysDepFormMap.parent_id=root&SysDepFormMap.dept_state=0',
		checkbox : true,
		checkValue:'dept_id',
		singleCheck:true,
		serNumber : false
	});
	
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		searchParams["sysDepFormMap.dept_name"] = "%"+searchParams["sysDepFormMap.dept_name"]+"%"
		grid.setOptions({
			data : searchParams
		});
	});
	
	//配置话务
	$("#phoneSetting").click(function(){
		var cbox = grid.getSelectedCheckbox();
		if (cbox == "") {
			tips("请选择需要配置的项",1);
			return;
		}
		var s = CommnUtil.ajax(rootPath+"/system/dept/judgeDeptType.shtml", {
			dids : cbox.join(",")
		}, "json");
		if(s==false){
			msg("警告","请只能选择公司","warning");
		}else{
			showModal("phonesetting","话务中心配置", rootPath+"/system/phoneSetting/phoneSettingShow.shtml?dept_ids="+cbox.join(","),500,180)
		}
		
	});
	
	$("#addDept").click(function() {
		var deptId =$("#parent_id").val();
		if (deptId == "" || deptId == null) {
			  Wind.use("artDialog", "iframeTools", function () {
				  art.dialog('请选择所选公司或部门!').time(2);
			  });
			return;
		}
		showModal("dept_info","新增", rootPath + "/system/dept/info.shtml?parent_id="+deptId,680,450);
	});
	$("#editDept").click(function() {
		var cbox = grid.getSelectedCheckbox();
		if (cbox.length > 1 || cbox == "") {
			tips("只能选中一个",1);
			return;
		}	
		showModal("dept_info","编辑", rootPath + "/system/dept/info.shtml?dep_id=" + cbox,680,450);
	});
	$("#delDept").click(function() {
		var cbox = grid.getSelectedCheckbox();
		if (cbox == "") {
			tips("请选择需要禁用的项",1);
			return;
		}
		confirm('你确定要禁用吗？', function() {
			var url = rootPath + '/system/dept/delete.shtml';
			var s = CommnUtil.ajax(url, {
				ids : cbox.join(",")
			}, "json");
			if(s=="success"){
				tips("操作成功",1);
				loadData();
			}else{
				tips("操作失败",1);
			}
		});
	});
	loadDictTree();
});

function loadDictTree(){
	//加载字典的树
	$("#deptTree").load(rootPath+"/system/dept/deptTree.shtml");
}



//重新加载数据
function loadData(){
	var searchParams = $("#searchForm").serializeJson();// 初始化传参数
	searchParams["sysDepFormMap.dept_name"] = "%"+searchParams["sysDepFormMap.dept_name"]+"%"
	grid.setOptions({
		data : searchParams
	});
	loadDictTree();
}



//点击跳转
function checkNode(data){
	chek = data.id;
	$("#parent_id").val(chek);
	grid.setOptions({								 
		jsonUrl :  rootPath + '/system/dept/findByPage.shtml?sysDepFormMap.parent_id='+data.id
	});
	
}
